package com.ruoyi.common.utils;

public class DistanceUtils {

    private static final double EARTH_RADIUS = 6371000; // 地球半径，单位为米
    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);

        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
            Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        double distance = EARTH_RADIUS * c;
        return distance;
    }

    public static void main(String[] args) {
        double lat1 = 32.028549; // 第一个位置的纬度
        double lon1 = 118.74153; // 第一个位置的经度
        double lat2 = 32.022984; // 第二个位置的纬度
        double lon2 = 118.740137; // 第二个位置的经度

        double distance = calculateDistance(lat1, lon1, lat2, lon2);
        System.out.println("两个位置之间的距离为：" + distance + "米");
    }
}
